package com.style.staticed;


import com.style.core.Event;
import com.style.core.IMouse;
import com.style.core.Mouse;
import com.style.core.MouseEventType;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;


/**
 * mouse静态代理
 */
public class MouseStaticedProxy {
    private IMouse target;

    public MouseStaticedProxy(IMouse target) {
        this.target = target;
    }

    //Map相当于是一个注册器
    protected Map<Enum, Event> events = new HashMap<Enum, Event>();

    public void addLisenter(Enum eventType, Object target, Method callback) {
        //注册事件
        //用反射调用这个方法
        events.put(eventType, new Event(target, callback));
    }

    public void trigger(Event e) {
        e.setSource(this);
        e.setTime(System.currentTimeMillis());

        try {
            e.getCallback().invoke(e.getTarget(), e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public void trigger(Enum call) {
        if (!this.events.containsKey(call)) {
            return;
        }
        trigger(this.events.get(call).setTrigger(call.toString()));
    }


    public void click() {
        target.click();
        trigger(MouseEventType.ON_CLICK);

    }

    public void doubleClick() {
        target.doubleClick();
        trigger(MouseEventType.ON_DOUBLE_CLICK);

    }

    public void up() {
        target.up();
        trigger(MouseEventType.ON_UP);

    }

    public void down() {
        target.down();
        trigger(MouseEventType.ON_DOWN);

    }

    public void wheel() {
        target.wheel();
        trigger(MouseEventType.ON_WHEEL);

    }


    public void move() {
        target.move();
        trigger(MouseEventType.ON_MOVE);

    }

    public void over() {
        target.over();
        trigger(MouseEventType.ON_OVER);

    }
}
